package com.dmap.community.service.community.impl;

import com.dmap.community.bo.theme.ShowThemeBo;
import com.dmap.community.dao.ThemeCollectMapper;
import com.dmap.community.po.ThemeCollect;
import com.dmap.community.po.ThemeCollectExample;
import com.dmap.community.service.community.CollectService;
import com.dmap.community.service.community.ThemeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * \* User: silent
 * \* Date: 14/11/17 Time: 10:51
 * \* Description:
 * \
 */
@Service
public class CollectServiceImpl implements CollectService {
    @Resource
    private ThemeCollectMapper themeCollectMapper;


    @Resource
    private ThemeService themeService;

    @Override
    public int countCollectThemeNumbersByUserId(long userId) {

        ThemeCollectExample themeCollectExample = new ThemeCollectExample();
        themeCollectExample.createCriteria().andUserIdEqualTo(userId);
        return themeCollectMapper.countByExample(themeCollectExample);
    }

    @Override
    public int countCollectThemeNumbersByThemeId(long themeId) {
        ThemeCollectExample themeCollectExample = new ThemeCollectExample();
        themeCollectExample.createCriteria().andThemeIdEqualTo(themeId);
        return themeCollectMapper.countByExample(themeCollectExample);
    }

    @Override
    public int deleteCollectByThemeId(long themeId, long userId) {
        ThemeCollectExample themeCollectExample = new ThemeCollectExample();
        ThemeCollectExample.Criteria criteria = themeCollectExample.createCriteria();
        criteria.andThemeIdEqualTo(themeId);
        if (userId != 0) {
            criteria.andUserIdEqualTo(userId);
        }
        return themeCollectMapper.deleteByExample(themeCollectExample);
    }

    @Override
    public int insertCollect(long themeId, long userId) {
        ThemeCollect themeCollect = new ThemeCollect();
        themeCollect.setCreatetime(System.currentTimeMillis() / 1000);
        themeCollect.setThemeId(themeId);
        themeCollect.setUserId(userId);
        return themeCollectMapper.insertSelective(themeCollect);
    }

    @Override
    public List<ShowThemeBo> showThemeListByUserId(long userId, int pageIndex, int pageSize) {
        List<ShowThemeBo> showThemeBoList;
        ThemeCollectExample themeCollectExample = new ThemeCollectExample();
        themeCollectExample.setLimitStart((pageIndex - 1) * pageSize);
        themeCollectExample.setLimitEnd(pageSize);
        themeCollectExample.setOrderByClause("create_time desc");
        themeCollectExample.createCriteria().andUserIdEqualTo(userId);
        List<ThemeCollect> themeCollectList = themeCollectMapper.selectByExample(themeCollectExample);
        List<Long> themeIds = new ArrayList<>(10);
        themeCollectList.forEach(themeCollect -> themeIds.add(themeCollect.getThemeId()));
        showThemeBoList = themeService.selectThemeListByThemeIds(themeIds);
        return showThemeBoList;
    }


}
